

//index.js
var App = getApp();

var ApiUrl = App.data.ApiUrl;

var Common = require('../../utils/common.js');

Page({
    data: {
        uname: '',
        SMCode: '',
        SMCTime: 60,
        SMCTimes: true,
        SMCode_Lock: false,
        SMCode_Msg: '获取验证码',
        isHideAdd: true,
        drinkingLevel: '1-5两',
        drinkingLevels: '1-5两',
        consultant_mobile: '',
        mobile: false,
        consultant_List: {},
        consultant: false,
        consultants: {},
        fromid: 0,
    },
    onLoad: function (options) {
        var zhuname = wx.getStorageSync('zhuname');
        wx.setNavigationBarTitle({
            title: zhuname + '注册'
        })

        this.setData({
            fromid: options.id,
        })
        /**顾问详情页转发过来时带的缓存 */
        var consultant_storge = wx.getStorageSync('consultant_storge');
        if (consultant_storge) {
            wx.removeStorageSync('consultant_storge');
            this.setData({
                consultants: consultant_storge,
                consultant: !this.data.consultant
            })
        }

        var openid = wx.getStorageSync('openid');
        this.setData({ openid: openid });
    },
    //选择顾问
    consultants: function () {
        Common.PageJump('pages/register/consultant/index')
    },
    updaterealName: function (e) {
        this.setData({
            realName: e.detail.value
        })
    },
    updateSMCode: function (e) {
        this.setData({
            SMCode: e.detail.value
        })
    },
    // 更新用户名
    updateuname: function (e) {
        var page = this;
        var uname = e.detail.value;
        var phonetest = /^0?(13|14|15|18|17)[0-9]{9}$/;
        page.setData({
            uname: e.detail.value
        })
        if (phonetest.test(uname)) {

            wx.request({
                url: ApiUrl + 'AppMaster/AppletRegister/checkIfAlreadyBeenConsultant',
                data: {
                    uname: uname,
                },
                method: 'post',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                success: function (res) {
                    console.log(res.data);
                    if (res.data.result == 0) {
                        if (res.data.data.status != 1) {
                            Common.Alert('提示', res.data.data.msg);
                        } else {
                            page.setData({
                                mobile: true,
                            })
                            if (page.data.SMCTime < 60) {
                                page.setData({
                                    SMCTime: 0,
                                })
                            }
                        }

                    } else {
                        Common.Alert('提示', res.data.message);
                        page.setData({
                            mobile: false,
                        })
                    }
                }
            })
        } else {
            page.setData({
                mobile: false,
            })
        }
    },
    getSMCode: function (e) {
        console.log(455465)
        var page = this;
        var uname = this.data.uname;
        var mobile = this.data.mobile;
      var phonetest = /^0?1\d{10}$/;

        page.setData({
            SMCode_Lock: true
        });
        if (!uname) {
            Common.Alert('登录提示', '请输入手机号码');
            return false;
        } else if (!mobile || !phonetest.test(uname)) {
            Common.Alert('登录提示', '请输入正确的手机号码');
            return false;
        }

        if (page.data.SMCTime == 60) {
            // 获取验证码
            wx.request({
                url: ApiUrl + '/AppMaster/AppletRegister/qrcSendCode',
                data: {
                    uname: uname,
                },
                method: 'post',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                success: function (res) {
                    console.log(res.data);

                    if (res.data.result == 0) {
                        if (res.data.data.status == 1) {
                            wx.showToast({
                                title: '验证码已发送',
                                icon: 'success',
                                duration: 2000
                            })

                            // 倒计时显示
                            page.SMCode_Msg();
                        }
                        else {
                            page.setData({
                                SMCTime: 60,
                                SMCode_Lock: false,
                                SMCode_Msg: '获取验证码'
                            })
                            Common.Alert('错误提示', res.data.data.msg);
                        }

                    } else {
                        page.setData({
                            SMCTime: 60,
                            SMCode_Lock: false,
                            SMCode_Msg: '获取验证码'
                        })
                        Common.Alert('错误提示', res.data.message);
                    }

                }
            })
        }
    },
    //验证顾问手机号
    consultant_mobiles: function (e) {
        var page = this;
        var mobile = e.detail.value;
        var phonetest = /^0?(13|14|15|18|17)[0-9]{9}$/;
        if (phonetest.test(mobile)) {
            page.setData({
                consultant_mobile: e.detail.value
            })
            wx.request({
                url: ApiUrl + '/AppMaster/AppletRegister/checkIfConsultantExist',
                data: {
                    consultant_mobile: mobile,
                },
                header: {
                    'content-type': 'json'
                },
                success: function (res) {
                    console.log(res.data);
                    if (res.data.result == 0) {
                        if (res.data.data.status == 'n') {
                            Common.Alert('提示', '该用户不可用！');
                        }
                    }
                }
            })
        }
    },
    logn: function () {
        // Common.PageJump('pages/login/login')
        Common.PageJump('pages/my/logintype/logintype')
    },
    register: function () {
        var page = this;
        var openid = page.data.openid;
        var consultant = page.data.consultant;
        var consultants = page.data.consultants;
        var SMCode = page.data.SMCode;
        var uname = page.data.uname;
        var realName = page.data.realName;
        var drinkingLevels = page.data.drinkingLevels;



        if (!consultant) {
            Common.Alert('注册提示', '请选择一位顾问');
            return false;
        } else if (!uname) {
            Common.Alert('注册提示', '请输入手机号');
            return false;
        } else if (!realName) {
            Common.Alert('注册提示', '请输入真实姓名');
            return false;
        } else if (!SMCode) {
            Common.Alert('注册提示', '请输入验证码');
            return false;
        }
        var page = this;
        Common.my_request({
            url: '/AppMaster/AppletRegister/appletRegister',
            data: {
                consultant_mobile: consultants.mobile,
                uname: uname,
                realName: realName,
                code: SMCode,
                drinking_capacity_level: drinkingLevels,
                agreement: 1,
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'post',
            success: function (res) {
                if (res.data.result == 0) {
                    wx.showToast({
                        title: '注册成功',
                        icon: 'success',
                        duration: 2000
                    })
                    console.log('res:******' +res);           
                    console.log('token:******'+res.data.data.token);
                    // 设置token
                    wx.setStorageSync('token', res.data.data.token);
                    Common.when_user_change_clear_the_storage(function(){
                        setTimeout(function () {
                            wx.switchTab({
                                url: '/pages/index/index'
                            })
                        }, 1000)
                    });
                } else {
                    Common.Alert('提示', res.data.msg);
                }
            }
        })

    },
    SMCode_Msg: function () {
        console.log('SMCode_Msg');
        var page = this;
        var SMCTimers;
        var SMCTime = page.data.SMCTime;
        var SMCode_Msg = page.data.SMCode_Msg;
        var SMCode_Lock = page.data.SMCode_Lock;

        if (SMCTime > 1) {

            SMCTime--;
            SMCode_Msg = '重新获取(' + SMCTime + ')';
            page.setData({
                SMCTime: SMCTime,
                SMCode_Msg: SMCode_Msg,
                SMCTimes: false,
            })

            // 定时器执行
            SMCTimers = setTimeout(function () {
                page.SMCode_Msg();
            }, 1000);

        } else {
            clearTimeout(SMCTimers);
            page.setData({
                SMCTime: 60,
                SMCode_Lock: false,
                SMCode_Msg: '获取验证码',
                SMCTimes: true,
            })
        }

    },
    drinkingLevel: function () {
        var page = this;
        page.setData({
            isHideAdd: !page.data.isHideAdd
        })
    },
    chooseDrinkingLevel: function (e) {
        var item = e.currentTarget.id;
        this.setData({
            drinkingLevel: item,
        })
    },
    drinkingLevelOK: function (e) {
        var page = this;
        var item = e.currentTarget.id;
        page.setData({
            drinkingLevels: page.data.drinkingLevel,
            isHideAdd: !page.data.isHideAdd
        })
    },
    showModal: function () {
        // 显示遮罩层
        var animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        })
        this.animation = animation
        animation.translateY(300).step()
        this.setData({
            animationData: animation.export(),
            showModalStatus: true
        })
        setTimeout(function () {
            animation.translateY(0).step()
            this.setData({
                animationData: animation.export()
            })
        }.bind(this), 200)
    },
    hideModal: function () {
        // 隐藏遮罩层
        var animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        })
        this.animation = animation
        animation.translateY(300).step()
        this.setData({
            animationData: animation.export(),
        })
        setTimeout(function () {
            animation.translateY(0).step()
            this.setData({
                animationData: animation.export(),
                showModalStatus: false
            })
        }.bind(this), 200)
    },
    fromids_index:function(){
        setTimeout(function () {
            wx.switchTab({
                url: '/pages/index/index'
            })
        }, 1000)
    }
})